package com.personalblog.controller;

import com.personalblog.po.Comment;
import com.personalblog.po.User;
import com.personalblog.service.BlogService;
import com.personalblog.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;

/**
 * @author jxau_scf
 * @date 2021/3/31 - 18:53
 */
@Controller
public class CommentController {
    @Autowired
    private CommentService commentService;
    @Autowired
    private BlogService blogService;

    @GetMapping("/comments/{blogId}")
    public String comments(@PathVariable int blogId, Model model){
        model.addAttribute("comments",commentService.listCommentByBlogId(blogId));
        return "blog :: commentList";
    }

    @PostMapping("/comments")
    public String post(Comment comment, HttpSession httpSession){
        Integer blogId = comment.getBlog().getBlogId();
//        comment.setBlog(blogService.findBlogById(blogId));
        User user = (User) httpSession.getAttribute("user");
        if (user != null){
            comment.setCommentAvatar(user.getUserAvatar());
            comment.setAdminComment(true);
        }else {
            comment.setCommentAvatar("/images/avatar.png");
        }
        System.out.println("comments请求传来的参数comment========>"+comment);
        commentService.saveComment(comment);
        return "redirect:/comments/" + blogId;
    }
}
